package com.xiaoyu.media.matisse.internal.entity

import android.content.Context
import android.net.Uri
import android.os.Parcel
import android.os.Parcelable
import com.xiaoyu.media.matisse.MimeType
import com.xiaoyu.media.matisse.internal.utils.PathUtils

class SelectionResult : Parcelable {

    val mediaList: MutableList<SelectionItem> = mutableListOf()

    private constructor(context: Context, items: List<Item>?) {
        items ?: return
        for (item in items) {
            val path = PathUtils.getPath(context, item.contentUri ?: Uri.EMPTY)
            mediaList.add(SelectionItem(item.mimeType, path, item.duration))
        }
    }

    private constructor(`in`: Parcel) {
        `in`.readTypedList(mediaList, SelectionItem.CREATOR)
    }

    private constructor(path: String?) {
        path ?: return
        mediaList.add(SelectionItem(MimeType.JPEG.toString(), path))
    }

    fun isNotEmpty(): Boolean {
        return mediaList.isNotEmpty()
    }

    override fun writeToParcel(dest: Parcel?, flags: Int) {
        dest?.writeTypedList(mediaList)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object {
        @JvmField
        val CREATOR = object : Parcelable.Creator<SelectionResult> {
            override fun createFromParcel(source: Parcel): SelectionResult {
                return SelectionResult(source)
            }

            override fun newArray(size: Int): Array<SelectionResult?> {
                return arrayOfNulls(size)
            }
        }

        fun createFromItemList(context: Context, list: List<Item>?): SelectionResult {
            return SelectionResult(context, list)
        }

        fun createFromCapture(path: String?): SelectionResult {
            return SelectionResult(path)
        }
    }
}

class SelectionItem : Parcelable {

    val mimeType: String?
    val path: String?
    val duration: Long

    constructor(mimeType: String?, path: String?, duration: Long = 0) {
        this.mimeType = mimeType
        this.path = path;
        this.duration = duration;
    }

    private constructor(`in`: Parcel) {
        this.mimeType = `in`.readString()
        this.path = `in`.readString()
        this.duration = `in`.readLong();
    }

    val isImage: Boolean
        get() = MimeType.isImage(mimeType)

    val isGif: Boolean
        get() = MimeType.isGif(mimeType)

    val isVideo: Boolean
        get() = MimeType.isVideo(mimeType)

    companion object {
        @JvmField
        val CREATOR = object : Parcelable.Creator<SelectionItem> {
            override fun createFromParcel(`in`: Parcel): SelectionItem {
                return SelectionItem(`in`)
            }

            override fun newArray(size: Int): Array<SelectionItem?> {
                return arrayOfNulls(size)
            }
        }
    }

    override fun writeToParcel(dest: Parcel?, flags: Int) {
        dest?.writeString(mimeType)
        dest?.writeString(path)
        dest?.writeLong(duration)
    }

    override fun describeContents(): Int {
        return 0
    }
}